package com.srwl.mytx.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.snackbar.Snackbar;
import com.srwl.mytx.Constant;
import com.srwl.mytx.R;
import com.srwl.mytx.activity.LoginActivity;
import com.srwl.mytx.adapter.LiveMessageAdapter;
import com.srwl.mytx.domain.LiveRoom;
import com.srwl.mytx.domain.Message;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.parse.UserProfileManager;
import com.srwl.mytx.utils.IDUtils;
import com.srwl.mytx.utils.Utils;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wei on 2016/6/3.
 */
public class RoomMessagesView extends RelativeLayout {

    LiveMessageAdapter adapter;

    RecyclerView listview;
    EditText editview;
    Button sendBtn;
    View sendContainer;
    SwitchButton sb_barrage;
    List<Message> messages = new ArrayList<>();
    private long lastTime = 0;
    public boolean isBarrageShow = false;
    private MessageViewListener messageViewListener;
    private User loginUserInfo;
    private LiveRoom liveRoom;


    public RoomMessagesView(Context context) {
        super(context);
        init(context, null);
    }

    public RoomMessagesView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoomMessagesView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public void setData(Message message) {
        messages.add(message);

        // adapter.notifyItemInserted(messages.size() - 1);
        ((Activity) getContext()).runOnUiThread(new Runnable() {
            @Override
            public void run() {

                adapter.notifyDataSetChanged();
                listview.smoothScrollToPosition(adapter.getItemCount() - 1);
            }
        });


    }

    private void init(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.widget_room_messages, this);
        listview = findViewById(R.id.listview);
        editview = findViewById(R.id.edit_text);
        sendBtn = findViewById(R.id.btn_send);
        sendContainer = findViewById(R.id.container_send);
        sb_barrage = findViewById(R.id.sb_barrage);
        editview.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(editable.toString())) {
                    sendBtn.setEnabled(false);
                } else {
                    sendBtn.setEnabled(true);
                }

            }
        });
        setLoinUserInfo();
    }

    public void setLoinUserInfo() {
        loginUserInfo = UserProfileManager.getInstance().getLoginUserInfo();
    }

    public EditText getInputView() {
        return editview;
    }

    public void init(LiveRoom liveRoom) {
        this.liveRoom = liveRoom;
        adapter = new LiveMessageAdapter(getContext(), messages);
        listview.setLayoutManager(new LinearLayoutManager(getContext()));
        listview.setAdapter(adapter);
        sendBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                long currentTime = System.currentTimeMillis();
                if (currentTime - lastTime < 1000) {
                    return;
                }
                lastTime = currentTime;
                if (Utils.isTempUser(UserProfileManager.getInstance().getLoginUserInfo())) {
                    Utils.hideKeyboard(editview);
                    getContext().startActivity(new Intent(getContext(), LoginActivity.class).putExtra("EXTRA_CONTENT", "发消息需要先登录"));
                    return;
                }
                if (TextUtils.isEmpty(editview.getText())) {
                    //Toast.makeText(getContext(), "文字内容不能为空！", Toast.LENGTH_SHORT).show();
                    Snackbar.make(sendBtn, "文字内容不能为空！", Snackbar.LENGTH_LONG).show();
                    return;
                }
                if (messageViewListener != null) {
                    String content = editview.getText().toString();
                    messageViewListener.onMessageSend(genMessage(content));
                    editview.setText("");
                }
            }
        });


        sb_barrage.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                isBarrageShow = isChecked;
            }
        });
    }

    private Message genMessage(String content) {
        Message message = new Message();
        message.setId(IDUtils.genOrderId());
        // message.setuId(myUserInfo.getuId());
        message.setTitle(loginUserInfo.getUsername());
        message.setContent(content);
        message.setType(Constant.LIVE_ROOM_MESSAGE);
        if (isBarrageShow) {
            message.setSubType(Constant.IM_TYPE_LIVE_ROOM_CHART_BARRAGE_MESSAGE);
            message.setIcon(loginUserInfo.getAvatar());
        } else {
            message.setSubType(Constant.IM_TYPE_LIVE_ROOM_CHART_MESSAGE);
        }
        //  message.setExtraData(JSONObject.toJSONString(liveRoom));
        message.setToId(liveRoom.getId());
        message.setFromId(loginUserInfo.getuId());
        return message;
    }

    public void setShowInputView(boolean showInputView) {
        if (showInputView) {
            sendContainer.setVisibility(View.VISIBLE);
        } else {
            Utils.hideKeyboard(editview);
            //sendContainer.setVisibility(View.INVISIBLE);
        }
    }

    public void hideInputView() {
        sendContainer.setVisibility(View.INVISIBLE);
    }

    public boolean isInputViewDisplay() {
        return sendContainer.getVisibility() == VISIBLE;
    }

    public void refresh() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public void refreshSelectLast() {
        if (adapter != null) {
            adapter.notifyDataSetChanged();
            listview.smoothScrollToPosition(adapter.getItemCount() - 1);
        }
    }

    public void setMessageViewListener(MessageViewListener messageViewListener) {
        this.messageViewListener = messageViewListener;
    }

    public interface MessageViewListener {
        void onMessageSend(Message message);

    }


}
